<?php
/**  
Plugin Name: WP Wakoopa Shortcode
Plugin URI: http://blog.kaizeku.com/projects/wordpress/shortcode#wp-wakoopa
Description: Show your most used software from <a href="http://wakoopa.com">Wakoopa</a> streams via JSON.
Version: 1.1
Author: Avice Devereux (ChaosKaizer)
Author URI: http://blog.kaizeku.com
License: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License v2 
*/

function wp_wakoopa_shortcode($attr, $username = 'chaoskaizer')
{
	if (empty($username)){
		return false;
	}
	$attribs = array(
		'limit' 	=> 5,			
		'heading'	=> 1,
		'subtitle' => 1,
		'info' => 1,
		'icons'=>1,
		'class' => 'wp_wakoopa_sc',	
	);		
		
	extract(shortcode_atts($attribs,$attr));
		
	
	$limit = (int) $limit;
	$limit = ($limit >= 31) ? 30 : $limit;	
		
	$src = 'http://api.wakoopa.com/'.$username.'/';
	$src .= 'most_used.json?callback=wp_wakoopa_shortcode&amp;limit='.$limit;
	
	$htm ="\n";	
	
	if ($heading){
		$uid = '<a href="http://wakoopa.com/'.$username.'" class="wakoopa_link">'.$username.'</a>';
		
		$htm .= '<h2> '.sprintf(__("%s&apos;s Most used software"),ucfirst($uid)).'</h2>'."\n";
	
	}
	
	if ($subtitle){
		$htm .= '<h5> '.sprintf(__("Top %d software"),$limit).'</h5>'."\n";
	}
	
	$htm .= '<ul id="wakoopa_sc" class="xoxo hlisting"></ul>'."\n";
	
	$src_param = '?wp_wakoopa_shortcode=js&amp;info='.$info.'&amp;icons='.$icons;
	$src_main = get_bloginfo('url').'/'.PLUGINDIR.'/'.plugin_basename(__FILE__).$src_param;
	$htm  .= '<script type="text/javascript" src="'.$src_main.'" charset="utf-8"></script>'."\n";  	
	$htm .= '<script type="text/javascript" src="'.$src.'" charset="utf-8"></script>'."\n";	
		
	$output = apply_filters('wp_wakoopa_shortcode',$htm);
		
	return $output;		
}

if ($_GET['wp_wakoopa_shortcode'] == 'js'){
 header('Content-type: text/javascript');
?>	

function wp_wakoopa_shortcode(arr){
 var wk, root, c ,txt, max;
 root = document.getElementById('wakoopa_sc');
 c = function(tag){return document.createElement(tag);};
 txt = function(text){ return document.createTextNode(text); };  
 max = arr.length;
 for (var i=0;i < max; i++) {
  wk = arr[i].software; 	     
    var software = c('a');    
    software.setAttribute('href',wk.complete_url);
    software.setAttribute('class','url fn');
    software.setAttribute('rel','external');
    software.setAttribute('title',wk.name);
    software.appendChild(txt(wk.name)); 	
	var span = c('span'); 
	span.setAttribute('class','vcard product uid');	
	span.appendChild(software);	
	var cat = c('a');		
	cat.setAttribute('href',wk.category.complete_url);		
	cat.appendChild(txt(wk.category.name.replace(/&amp;/,"&")));	
	var spancat = c('span');
	spancat.setAttribute('class','category');
	spancat.appendChild(txt(' ('));
	spancat.appendChild(cat);
	spancat.appendChild(txt(') '));	
	var usage = c('span');
	usage.setAttribute('class','active_hours');
	var husage = Math.floor( wk.active_seconds / 36000);
  usage.appendChild( txt(' - used for ' + husage + ' hours.') );
	var li = c('li');   
	li.appendChild(software);
	li.appendChild(spancat);
	li.appendChild(usage);	
	<?php if($_GET['info'] == 1) : ?>
	var p = c('p');
  var span2 = c('span');
  	  span2.setAttribute('class','org');  	  
  var developer = c('a');
    developer.setAttribute('href',wk.developer.complete_url);
    developer.appendChild(txt(wk.developer.name));    
    span2.appendChild(developer);		
		var span3 = c('span');
		span3.setAttribute('style','display:block');	
	span3.appendChild(txt('Developed by '));
	span3.appendChild(developer);		
		p.appendChild(span3);	
		p.appendChild(txt(wk.category.description));	
		li.appendChild(p);
	<?php endif; ?>	
	<?php if($_GET['icons'] == 1) : ?>
	li.setAttribute('style','list-style-image:url('+wk.complete_thumb_url+');margin-left:18px');
	<?php endif; ?>
    root.appendChild(li);
 }
}	
<?php	
 exit();
}

if (function_exists('add_shortcode')){
	add_shortcode('wakoopa','wp_wakoopa_shortcode');
}
?>